package calvin.study.rabbitmq.config.mq;

import java.util.HashMap;
import java.util.Map;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 延迟交换机、队列、绑定关系配置
 * 
 * @author wh
 *
 */
@Configuration
public class TTLMqConfig {

	// 声名交换机,并指定一个交换机类型
	@Bean
	public DirectExchange ttlDirectExchange() {
		return new DirectExchange("ttl_direct_exchange", true, false);
	}

	// 声明过期队列
	@Bean
	public Queue ttlQueue() {
		// 声明队列过期时间
		Map<String, Object> args = new HashMap<>();
		args.put("x-message-ttl", 5000);
		return new Queue("ttl_queue1", true, false, false, args);
	}
	
	@Bean
	public Binding bindingTTL() {
		return BindingBuilder.bind(ttlQueue()).to(ttlDirectExchange()).with("ttl1");
	}

	// 声明队列
	@Bean
	public Queue ttlMessageQueue() {
		return new Queue("ttl_queue2", true);
	}
	
	@Bean
	public Binding bindingTTLMessage() {
		return BindingBuilder.bind(ttlMessageQueue()).to(ttlDirectExchange()).with("ttl2");
	}

	//声明过期队列，并将消息交给死信交换机
	@Bean
	public Queue ttlToDeadLetterQueue() {
		// 生命队列过期时间
		Map<String, Object> args = new HashMap<>();
		args.put("x-message-ttl", 5000);
		//过期消息投递到死信交换机
		args.put("x-dead-letter-exchange", "dead_letter_direct_exchange");
		//死信队列路由key
		args.put("x-dead-letter-routing-key", "deadletter");
		return new Queue("ttl_to_dead_letter_queue1", true, false, false, args);
	}
	
	//绑定交换机与队列，并设置路由key
	@Bean
	public Binding bindingTTL2DeadLetter() {
		return BindingBuilder.bind(ttlToDeadLetterQueue()).to(ttlDirectExchange()).with("ttl2dead");
	}

}
